<?php
namespace app\proxy\controller;
use think\Db;
use app\service\TechnService;
use think\Request;
use think\Session;

class Techn extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index(Request $request)
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [];
				if(!empty($params['title']))
				{
					$map['real_name'] = ['like','%'.$params['title'].'%'];
				}
				
				if(!empty($params['nickname']))
				{
					$map['nickname'] = ['like','%'.$params['nickname'].'%'];
				}
				
				if(!empty($params['mobile']))
				{
					$map['mobile'] = $params['mobile'];
				}
				
				$proxy = session('proxy');

                $proxy_id = $proxy['proxy_id'];

                if ($proxy['is_xian'] == 0){
                    $proxy_now = Db::name('proxy')->where('proxy_id',$proxy_id)->find();
                    $proxy_info = Db::name('Proxy')->where('shi',$proxy_now['shi'])->where('is_xian',1)->column('qu');
                    if (!empty($proxy_info)){
                        $map['county'] = ['not in',$proxy_info];
                    }
                    $map['city'] = ['=',$proxy_now['shi']];
                }else{
                    $proxy_info = Db::name('Proxy')->where("proxy_id = '$proxy_id'")->find();

                    $map['county'] = ['=',$proxy_info['qu']];
                }

                
				$count = Db::name('techn')->where($map)->count();
				$list_data = Db::name('techn')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						$list_data[$key]['save_url'] = url('Techn/saveinfo', ['id' => $value['id']]);
						//编辑url
						$list_data[$key]['sextitle'] = $value['sex']==1?'男':'女';
						if($value['work_status']==1){
						    $list_data[$key]['workmsg']='接单';
						}elseif($value['work_status']==2){
						    $list_data[$key]['workmsg']='休息';
						}else{
						    $list_data[$key]['workmsg']='下线';
						}
						$list_data[$key]['lhtitle'] = $value['is_lh']==1?'拉黑':'正常';
						$list_data[$key]['addr']=$value['province_name'].'-'.$value['city_name'].'-'.$value['county_name'];
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 服添添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		
			//查询
		$info = Db::name('techn')->where(['id' => $params['id']])->find();
			
		$this->assign('info',$info);

        $map = [
					'is_delete_time' => 0
				];
				
		$service = Db::name('service')->where($map)->select();
		
		$service_ids=explode(',',$info['service_ids']);
        $uptel=db::name('user')->where('id',$info['upid'])->value('mobile');

        $this->assign('uptel',$uptel);
		//编辑器文件存放地址
		$this->assign('service_ids', $service_ids);
		$this->assign('service', $service);
		return $this->fetch();
	}

	/**
	 * 服务添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			
			$params['service_ids']=','.implode(',',$params['service_ids']).',';
			
			$params['self_img']=implode(',',$params['self_img']);
			
			$params['id_card_img']=implode(',',$params['id_card_img']);
			
			$params['upd_time'] = time();
            if(!empty($params['uptel'])){
                $params['upid']=db::name('user')->where('mobile',$params['uptel'])->value('id');
                $params['bdtime']=time();
            }else{
                $params['upid']=0;
                $params['bdtime']=0;
            }

            unset($params['uptel']);
			
			Db::name('techn')->where(['id'=>intval($params['id'])])->update($params);
			//是否成功
			$result['code'] = 0;
			$result['msg'] = "操作成功";
		}
		echo json_encode($result);
	}

	/**
	 * 服务删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			if(Db::name('techn')->where(['id' => intval($params['id'])])->delete())
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
    public function yjindex()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        if(request()->isPost())
        {
            $params = input();
            if(!empty($params['page']) && !empty($params['limit']))
            {
                $map = [];
                // $map['upid']=['>',0];


                if(!empty($params['mobile']))
                {
                    $id=db::name('user')->where('mobile',$params['mobile'])->value('id');
                    $map['user_id']=$id;
                }
                // print_r($map);exit;

                $count = Db::name('user_bd')->where($map)->count();
                $list_data = Db::name('user_bd')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
                if(!empty($list_data))
                {
                    //遍历处理
                    foreach($list_data as $key => $value)
                    {

                        $user=db::name('user')->where('id',$value['user_id'])->find();

                        $techn=db::name('techn')->where(['user_id'=>$value['bduid'],'upid'=>$value['user_id']])->find();

                        $users=db::name('user')->where(['id'=>$value['bduid'],'upid'=>$value['user_id']])->find();

                        $value['bdtime']=!empty($techn)?$techn['bdtime']:$users['bdtime'];

                        $list_data[$key]['save_url'] = url('Techn/yjsaveinfo', ['id' => $value['id']]);

                        $list_data[$key]['bdtime']=date('Y-m-d H:i:s',$value['bdtime']);

                        $list_data[$key]['upname']=$user['nickname'];

                        $list_data[$key]['uptel']=$user['mobile'];

                        $list_data[$key]['mobile']=!empty($techn)?$techn['mobile']:$users['mobile'];

                        $list_data[$key]['nickname']=!empty($techn)?$techn['nickname']:$users['nickname'];


                    }
                    $result['code'] = 0;
                    $result['msg'] = "请求成功";
                    $result['data'] = $list_data;
                    $result['count'] = $count;
                }else
                {
                    $result['code'] = -1;
                    $result['msg'] = "暂无数据";
                }
            }
            echo json_encode($result);
        }else
        {

            return $this->fetch();
        }
    }

    public function yjsaveinfo()
    {
        // 参数
        $params = input();
        $info = [];

        //查询
        $info = Db::name('user_bd')->where(['id' => $params['id']])->find();

        $this->assign('info',$info);


        return $this->fetch();
    }

    public function yjsave()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        if(request()->isPost())
        {
            $params = input();


            $params['upd_time'] = time();


            Db::name('user_bd')->where(['id'=>intval($params['id'])])->update($params);
            //是否成功
            $result['code'] = 0;
            $result['msg'] = "操作成功";
        }
        echo json_encode($result);
    }
}